package com.lin.cloud.disk.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.lin.cloud.disk.common.entity.request.AccountRequestDTO;
import com.lin.cloud.disk.entity.dto.LogicRemoveDTO;
import com.lin.cloud.disk.entity.dto.LogicRestoreDTO;
import com.lin.cloud.disk.entity.dto.PhysicistRemoveDTO;
import com.lin.cloud.disk.entity.pojo.Directory;
import com.lin.cloud.disk.entity.request.directory.GetDirectoryListRequest;
import com.lin.cloud.disk.entity.request.directory.IsExistParentDirectoryRequest;
import com.lin.cloud.disk.entity.request.directory.RemoveDirectoryRequest;
import com.lin.cloud.disk.entity.request.directory.RestoreDirectoryRequest;
import com.lin.cloud.disk.mapper.DirectoryMapper;
import com.lin.cloud.disk.service.IDirectoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * (Directory)表服务实现类
 *
 * @author ljc
 * @since 2020-12-30 21:53:41
 */
@Service
@Slf4j
public class DirectoryServiceImpl implements IDirectoryService {

    @Resource
    private DirectoryMapper directoryMapper;


    /**
     * 创建目录
     *
     * @param directory
     */
    @Override
    public void createDirectory(Directory directory) {
        directory.setCreateTime(new Date());
        directory.setIsDelete(0);
        directoryMapper.insert(directory);
    }

    /**
     * 是否存在父目录
     * @param request
     * @return
     */
    @Override
    public boolean isExistParentDirectory(IsExistParentDirectoryRequest request){
        return directoryMapper.getParentDirectory(request) != null ? true: false;
    }

    /**
     * 获取目录列表
     * @param request
     * @return
     */
    @Override
    public List<Directory> getDirectoryByList(GetDirectoryListRequest request){
        return directoryMapper.getDirectoryByList(request);
    }

    /**
     * 修改目录名字
     * @param directory
     */
    @Override
    public int updateDirectoryName(Directory directory){
        directory.setUpdateTime(new Date());
        return directoryMapper.updateById(directory);
    }

    /**
     * 删除目录
     * @param request
     * @return
     */
    @Override
    public int logicRemoveDirectory(RemoveDirectoryRequest request){
        if (CollectionUtil.isEmpty(request.getIdList())){
            return 0 ;
        }
        List<String> idList = request.getIdList();
        LogicRemoveDTO logicRemoveDTO = new LogicRemoveDTO();
        logicRemoveDTO.setIdList(idList);
        logicRemoveDTO.setCompanyId(request.getUserDTO().getCompanyId());
        logicRemoveDTO.setUserId(request.getUserDTO().getId());
        logicRemoveDTO.setUpdateBy(request.getUserDTO().getUsername());
        logicRemoveDTO.setTime(new Date());
        return directoryMapper.logicRemoveDirectory(logicRemoveDTO);
    }

    /**
     * 物理删除目录
     * @param request
     */
    @Override
    public int physicistRemoveDirectory(RemoveDirectoryRequest request){
        if (CollectionUtil.isEmpty(request.getIdList())){
            return 0 ;
        }
        List<String> idList = request.getIdList();
        PhysicistRemoveDTO physicistRemoveDTO = new PhysicistRemoveDTO();
        physicistRemoveDTO.setIdList(idList);
        physicistRemoveDTO.setCompanyId(request.getUserDTO().getCompanyId());
        physicistRemoveDTO.setUserId(request.getUserDTO().getId());
        return directoryMapper.physicistRemoveDirectory(physicistRemoveDTO);
    }

    /**
     * 逻辑恢复目录
     * @param request
     * @return
     */
    @Override
    public int logicRestoreDirectory(RestoreDirectoryRequest request){
        if (CollectionUtil.isEmpty(request.getIdList())){
            return 0 ;
        }
        List<Long> idList = request.getIdList();
        LogicRestoreDTO logicRestoreDTO = new LogicRestoreDTO();
        logicRestoreDTO.setIdList(idList);
        logicRestoreDTO.setCompanyId(request.getUserDTO().getCompanyId());
        logicRestoreDTO.setUserId(request.getUserDTO().getId());
        logicRestoreDTO.setUpdateBy(request.getUserDTO().getUsername());
        logicRestoreDTO.setTime(new Date());
        return directoryMapper.logicRestoreDirectory(logicRestoreDTO);
    }
}